package com.woniuxy.myshop.contrloller;

import com.woniuxy.myshop.entity.User;
import com.woniuxy.myshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService ;

    @RequestMapping("/login")
    public String login(String username, String password,
                        Map map, HttpSession session){
        //Map  Model  ModelMap  request  都是操作 request作用域
        //根据用户名 查询一个用户对象
        User user = userService.login(username);
        if (user == null) {
            //账号不存在
            map.put("errorInfo","账号不存在!");
            return "login" ;
        }else {
            if (user.getPassword().equals(password)) {
                //登录成功
                session.setAttribute("userInfo",user);

                return "redirect:/product/list" ;
            }else {
                //密码错误
                map.put("errorInfo","密码错误!");
                return "login" ;
            }
        }
    }

    @RequestMapping("/exit")
    public String exit(HttpSession session){
        session.invalidate();
        // 处理业务逻辑

        return "redirect:/login.html" ;
    }
}
